// import { PageLoading } from '@ant-design/pro-layout';

import getApiData from '@/services/cpasRequest';
import notify from 'devextreme/ui/notify';
import { useEffect, useRef, useState } from 'react';
import { history, useModel } from 'umi';
import PageLoading from '../components/PageLoading';
import CommonCpasTable from './components';
import './index.less';
import { getjudgmentInit, templateInitApi } from './service';
const Percentage = (props: any) => {
  const { initialState } = useModel('@@initialState');
  const { dbname, zcbfid, bsdw, sjnd } = initialState?.curPart;
  const { pathname, query }: any = history.location || {};
  const isAudit = useRef<any>(pathname?.includes('unaudit/Percentage') ? '未审' : '审定');
  const [spinLoad, setSpinLoad] = useState<boolean>(false);
  // const [spinAntTabs, setSpinAntTabs] = useState<boolean>(true);
  const [textLoad, settextLoad] = useState<string>('');
  const refsCommonCpasTable = useRef<any>(null);
  const basePath = '\\Tools';
  let tableNameGroup = `试算比率分析_${isAudit.current}`;
  let propsData: any = {
    tableName: `试算比率分析_${isAudit.current}`,
    titleName: `试算比率分析_${isAudit.current}`,
    WscwbbType: '资产类',
    origin: '审定财务',
    tabsIndex: 1,
    CPASTableType: 'fs',
  };
  const [excelParams, setExcelParams] = useState<any>([
    {
      cpastableParam: {
        tname: `试算比率分析_${isAudit.current}`,
        data_id: '',
        type: 'fs',
      },
      // tableTitle: "资产负债表",
      tableDate: ' ',
      isExportTableTitle: true,
      exportType: 'primary',
      isBatchExport: true,
    },
  ]);

  /**
   * 模板初始化接口
   * @param isAllInit
   * @param yearTableName
   * @param actionTabs
   * @returns
   */
  const getAuditReportTemplate = async (
    isAllInit: boolean,
    yearTableName: any,
    actionTabs: any,
    cb?: any,
  ) => {
    try {
      const judg = await getjudgmentInit({
        dbname,
        type: 'fs',
        tableRealName: yearTableName,
      });
      if (judg.success) {
        if (judg.data?.count_number === 0) {
          const resTemp: any = await templateInitApi({
            dbName: dbname,
            templateFile: `${window?.main?.mainObject.getAppPath()}${basePath}\\TBAnalyisi.db`,
            type: 'fs',
            tableNames: [yearTableName],
          });

          // debugger;
          if (resTemp.success) {
            notify(`模板初始化成功`, 'success', 2000);
            // CopyFinancialIndicators
            await getApiData('CopyFinancialIndicators', { dbName: dbname });
            if (!refsCommonCpasTable.current) return;

            await refsCommonCpasTable.current.CPASInit2(yearTableName, true);
            await refsCommonCpasTable.current.reCumputer();
            //await refsCommonCpasTable.current.saveToServer();
          } else {
            await refsCommonCpasTable.current.CPASInit2(yearTableName, false);
            if (resTemp.msg) {
              notify(`${resTemp.msg}`, 'error', 2000);
            } else {
              notify(`模版初始化失败`, 'warning', 2000);
            }
          }
        } else {
          if (!refsCommonCpasTable.current) return;
          await refsCommonCpasTable.current.CPASInit2(yearTableName, false);
        }
      } else {
        notify(`模版是否初始化接口失败！`, 'error', 2000);
      }
    } catch (error) {
      console.log('err', error);
      notify(`获取判断是否初始化失败！`, 'error', 2000);
    }
  };

  const pageLoading = (r: boolean, textLoad = '加载中...') => {
    setSpinLoad(!r);
    settextLoad(textLoad);
  };
  useEffect(() => {
    setSpinLoad(true);

    getAuditReportTemplate(true, tableNameGroup, '');

    setSpinLoad(false);
  }, []);

  return (
    <div className="" style={{ height: '90vh' }}>
      <PageLoading isOpen={spinLoad} isMaskClose={false} width={200} content={textLoad} />
      <CommonCpasTable
        CPASTableProps={propsData}
        CPASTableTabName={tableNameGroup}
        pageLoading={pageLoading}
        excelParams={excelParams}
        ref={refsCommonCpasTable}
        tableNameGroup={tableNameGroup}
      />
      <p style={{ textAlign: 'right' }}>
        提示：置灰部分为系统自动取数计算的数据,未置灰部分为手动录入部分。
      </p>
    </div>
  );
};
export default Percentage;
